package zzl.com.fof;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.util.StringUtils;

import java.io.IOException;

/**
 * Created by EDZ on 2018/3/27.
 */
public class FofMapper extends Mapper<LongWritable,Text,Text,LongWritable>{

    Text mkey = new Text();
    LongWritable vaul = new LongWritable();
    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        String[] strs = value.toString().split(" ");
        //输入一行数据
        //迭代后两两输出
        //标注关系(0:直接好友,1:间接好友)
        //tom hadoop cat dog zzl
        for(int i =0 ;i<strs.length;i++){
          String str ="";
          str =comp(strs[0],strs[i]);
          mkey.set(str);
          vaul.set(0);
          context.write(mkey,vaul);
          for(int j =i+1;j<strs.length-1;j++){
            int tmp = i+1;
            str = comp(strs[tmp],strs[j+1]);
            mkey.set(str);
            vaul.set(1);
              context.write(mkey,vaul);
          }
        }
    }

    private String comp(String a1,String a2){
        if(a1.compareTo(a2)>0){
            return a2+"_"+a1;
        }else{
            return a1+"_"+a2;
        }
    }
}
